// goods/pages/addressList/addressList.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    where: 0
  },
  //选择收货地址
  toSettleAccounts(e) {
    const that = this;
    let index = e.currentTarget.dataset.index
    if (that.data.where == 1) {
      let pages = getCurrentPages(); //获取小程序页面栈
      let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
      beforePage.setData({
        address: that.data.dataList[index]
      })
      wx.navigateBack({
        delta: 1
      })
    }

  },
  toAddAddress(e) {
    const that = this;
    let addressId = e.currentTarget.dataset.address_id || '';
    wx.navigateTo({
      url: `/goods/pages/addAddress/addAddress?where=${that.data.where}&addressId=${addressId}`,
    })
  },
  //获取收货地址
  getData() {
    const that = this;
    let data = {
      coordinate: 'GCJ02'
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getAddressList, data).then(res => {
      wx.hideLoading()
      if (res.data.status == 1) {
        that.setData({
          dataList: res.data.result
        })
      }
    }).catch(err => {
      console.log(err, 888)
    })
  },
  //编辑
  edit(e) {
    // 把详细地址和门牌号拆分
    var strSum = e.currentTarget.dataset.address.indexOf('|');
    var strLength = e.currentTarget.dataset.address.length;
    // 门牌号数据
    var str = e.currentTarget.dataset.address.substring(strSum + 1, strLength);
    // 详细地址数据
    var place = e.currentTarget.dataset.address.substring(0, strSum);
    let id = e.currentTarget.dataset.id;
    let consignee = e.currentTarget.dataset.consignee;
    let latitude = e.currentTarget.dataset.latitude;
    let longitude = e.currentTarget.dataset.longitude;
    let address = place;
    let mobile = e.currentTarget.dataset.mobile;
    let houseNumber = str;
    let is_default = e.currentTarget.dataset.is_default;
    wx.navigateTo({
      url: `/package/pages/addSite/addSite?id=${id}&consignee=${consignee}&latitude=${latitude}&longitude=${longitude}&address=${address}&mobile=${mobile}&houseNumber=${houseNumber}&is_default=${is_default}`

    })

  },
  //删除
  delete(e) {
    const that = this;
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      content: '是否删除该地址？',
      success(res) {
        if (res.confirm) {
          let data = {
            id: id,
            token: app.globalData.token
          }
          wx.showLoading({
            title: '加载中...',
          })
          myRequest.EasyHttp.post(api.reqUrls.del_address, data).then(res => {
            if (res.data.status == 1) {
              that.getData()
            }
          }).catch(err => {
            console.log(err, 888)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toSettleAccounts(e) {
    const that = this;
    let index = e.currentTarget.dataset.index
    if (that.data.where == 1) {
      let pages = getCurrentPages(); //获取小程序页面栈
      let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
      beforePage.setData({
        address_id:that.data.dataList[index].address_id
      })
      beforePage.getCart2Data()
      wx.navigateBack({
        delta: 1
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this
    if (options.where) {
      that.setData({
        where: options.where
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const that = this
    that.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})